package com.twinkle.abmsspringboot.controller;

import com.twinkle.abmsspringboot.entity.Contacts;
import com.twinkle.abmsspringboot.entity.UserInfo;
import com.twinkle.abmsspringboot.service.ContactsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ABMS - SpringBoot
 * <p>controller 联系人处理控制器</p>
 *
 * @author Twinkle
 * @version 1.0.1
 * @since 30 Nov. 2020
 * @since 2 Aug. 2021
 */
@Slf4j
@Controller
public class ContactsController {

    @Autowired
    private ContactsService contactsService;

    @PostMapping("/selectLike")
    public String selectSContactLike(HttpServletRequest httpServletRequest, HttpSession session) {
        String search_name = httpServletRequest.getParameter("search_name");
        UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");
        session.setAttribute("listAll", contactsService.selectContactLike(userInfo.getUserinfo_id(),
                search_name));
        System.out.println("==== SelectLike -> \"" + search_name + "\"");
        return "main";
    }

    @RequestMapping("/selectAll")
    public String selectSContact(HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");
        log.info("receive session" + userInfo.toString());
        session.removeAttribute("listAll");
        List<Contacts> contactsListAll = contactsService.selectContact(userInfo.getUserinfo_id());
        for (Contacts contacts : contactsListAll) {
            log.debug(contacts.toString());
        }
        session.setAttribute("listAll", contactsListAll);
        log.info("selectAll -> success");
        return "redirect:/main.html";
    }

    @PostMapping("/insert")
    public String insertSContacts(HttpServletRequest httpServletRequest, HttpSession session) {
        System.out.println("\n==== Insert Contacts");
        Contacts contacts = new Contacts();
        UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");
        contacts.setContacts_id(Integer.parseInt(httpServletRequest.getParameter("coId")));
        contacts.setContacts_name(httpServletRequest.getParameter("coName"));
        contacts.setContacts_phone(httpServletRequest.getParameter("coPhone"));
        contacts.setContacts_email(httpServletRequest.getParameter("coEmail"));
        System.out.println("== " + contacts.getContacts_id() + "," + contacts.getContacts_name() + ","
                + contacts.getContacts_phone() + "," + contacts.getContacts_email());
        boolean isInsert = contactsService.insertContacts(contacts);
        if (isInsert) {
            System.out.println("==== Insert Success\n==== " + contacts.toString());
            session.setAttribute("listAll", contactsService.selectContact(userInfo.getUserinfo_id()));
        } else {
            System.out.println("==== Insert Failure");
        }
        return "main";
    }

    @PostMapping("/delete/{cid}")
    public String deleteSContacts(@PathVariable Integer cid, HttpSession session) {
        System.out.println("\n==== Delete Contacts");
        UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");
        boolean isDelete = contactsService.deleteContacts(cid);
        if (isDelete) {
            System.out.println("==== Delete Success\n==== " + cid);
            session.setAttribute("listAll", contactsService.selectContact(userInfo.getUserinfo_id()));
        } else {
            System.out.println("==== Delete Failure");
        }
        return "main";
    }

    @PostMapping("/update")
    public String updateSContacts(HttpServletRequest httpServletRequest) {
        Contacts contacts = new Contacts();
        contacts.setContacts_id(Integer.parseInt(httpServletRequest.getParameter("upId")));
        contacts.setContacts_name(httpServletRequest.getParameter("upName"));
        contacts.setContacts_phone(httpServletRequest.getParameter("upPhone"));
        contacts.setContacts_email(httpServletRequest.getParameter("upEmail"));
        boolean isUpdate = contactsService.updateContacts(contacts);
        if (isUpdate) {
            System.out.println("==== Update Success\n==== " + contacts.toString());
        } else {
            System.out.println("==== Update Failure");
        }
        return "main";
    }
}
